package com.zhihuitong.meeting.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.zhihuitong.meeting.utils.ActivityUtils;
import com.zhihuitong.meeting.view.CustomDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 作者： lilingjie
 * 时间 2017/8/22.
 * 文件描述：AC基类
 * 版本号：V 1.0
 **/

public abstract class BaseActivity extends AppCompatActivity {


    protected Context mContext;
    private Unbinder mUnbinder;
    protected CustomDialog mDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(getLayoutId());
        mUnbinder = ButterKnife.bind(this);
        mDialog = new CustomDialog(this);
        //保存activity
        ActivityUtils.addActivity(this);
        initData();
        initView();
    }

    /**
     * 返回子类布局id
     *
     * @return
     */
    protected abstract int getLayoutId();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        ActivityUtils.removeActivity(this);
    }


    /**
     * 初始化數據
     */
    protected abstract void initData();

    /**
     * 初始化View
     */
    protected abstract void initView();


    /**
     * 初始化 Toolbar
     */
    public void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnabled);
    }

    public void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, int resTitle) {
        initToolBar(toolbar, homeAsUpEnabled, getString(resTitle));

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void showLoading() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
    }


    protected void hidLoading() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();

        }
    }
}
